package com.findtech.threePomelos.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetDataCallback;
import com.avos.avoscloud.ProgressCallback;
import com.findtech.threePomelos.R;
import com.findtech.threePomelos.base.MyActionBarActivity;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.entity.BabyInfoEntity;
import com.findtech.threePomelos.net.NetWorkRequest;
import com.findtech.threePomelos.utils.MyCalendar;
import com.findtech.threePomelos.utils.ToastUtil;
import com.findtech.threePomelos.utils.Tools;

import java.text.ParseException;
import java.util.List;

/**
 * Created by zhi.zhang on 5/3/16.
 */
public class GetUserProtocolActivity extends MyActionBarActivity {

    private TextView userProtocolContent;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_protocol);
        MyApplication.getInstance().addActivity(this);
        setToolbar(getResources().getString(R.string.title_activity_get_user_protocol), true);
        userProtocolContent = (TextView) findViewById(R.id.tv_body);
        initProgressDialog();
        getUserProtocol();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    Handler mHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x75) {
                userProtocolContent.setText(Html.fromHtml((String) msg.obj));
            }
            if (msg.what == 0x74) {
                Log.d("ZZ", "msg.obj = " + msg.obj);
            }
        }
    };

    private void initProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("加载中，请稍后...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(true);
    }

    private void getUserProtocol() {
        progressDialog.show();
        AVQuery<AVObject> query = new AVQuery<>(NetWorkRequest.USER_PROTOCOL);
        query.findInBackground(new FindCallback<AVObject>() {
            public void done(List<AVObject> avObjects, AVException e) {
                if (e == null) {
                    if (avObjects.size() > 0) {
                        AVFile avFile = avObjects.get(0).getAVFile("userProtocol");
                        if (avFile == null) {
                            ToastUtil.showToast(GetUserProtocolActivity.this, "数据异常！");
                            return;
                        }
                        avFile.getDataInBackground(new GetDataCallback() {
                            public void done(byte[] data, AVException e) {
                                //process data or exception.
                                progressDialog.dismiss();
                                if (e == null) {
                                    String body = new String(data);
                                    mHandle.obtainMessage(0x75, body).sendToTarget();
                                } else {
                                    ToastUtil.showToast(GetUserProtocolActivity.this, "网络异常，请稍后再试！");
                                }
                            }
                        }, new ProgressCallback() {
                            @Override
                            public void done(Integer integer) {
                                Log.d("ZZ", "integer.intValue = " + integer.intValue());
                                mHandle.obtainMessage(0x74, integer).sendToTarget();
                            }
                        });
                    } else {
                        progressDialog.dismiss();
                        ToastUtil.showToast(GetUserProtocolActivity.this, "没有查到数据！");
                    }
                } else {
                    progressDialog.dismiss();
                    ToastUtil.showToast(GetUserProtocolActivity.this, "网络异常，请稍后再试！");
                }
            }
        });
    }
}
